package com.sc.nft.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.qiniu.storage.StreamUploader;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.dao.ProductStockDao;
import com.sc.nft.dao.StaticDataDao;
import com.sc.nft.entity.ProductStock;
import com.sc.nft.entity.StaticData;
import com.sc.nft.entity.dto.ListDetailsListDTO;
import com.sc.nft.entity.dto.RegisterNeckCollectionDTO;
import com.sc.nft.entity.vo.ListDetailsListVO;
import com.sc.nft.entity.vo.RegisterNeckCollectionVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.RegisterNeckCollectionService;
import com.sc.nft.service.UserInfoService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/registerNeckCollection")
@Api(tags = "注册领取藏品功能")
public class RegisterNeckCollectionController {
    private final RegisterNeckCollectionService registerNeckCollectionService;
    private final Redisson redisson;
    private final ProductStockDao productStockDao;
    private final StaticDataDao staticDataDao;

    @ApiOperation(value = "注册并领取藏品", notes = "注册并领取藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/registerCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<RegisterNeckCollectionVO> registerCollection(@Validated @RequestBody RegisterNeckCollectionDTO registerNeckCollectionDTO) {
        String lockKey = StrUtil.format("{}{}", RedisKeyEnum.REGISTER_COLLECTION.getKey(), registerNeckCollectionDTO.getUserTel());
        RLock lock = redisson.getLock(lockKey);
        RegisterNeckCollectionVO registerNeckCollectionVO = null;
        try {
            boolean b = lock.tryLock(10, TimeUnit.SECONDS);
            if (b) {
                registerNeckCollectionVO = registerNeckCollectionService.registerNeckCollection(registerNeckCollectionDTO.getUserTel(), registerNeckCollectionDTO.getSmsCode(), registerNeckCollectionDTO.getSmsId());
            }
        } catch (InterruptedException e) {
            throw new GlobalRunTimeException("活动太火爆了,在尝试一次吧");
        } finally {
            lock.unlock();
        }
        return Result.success(registerNeckCollectionVO);
    }

    @ApiOperation(value = "库存", notes = "库存", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/inventory", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result inventory() {
        StaticData staticData = staticDataDao.getByType("register_neck_collection");
        ProductStock productStock = productStockDao.getByProductId(Convert.toLong(staticData.getValue()));
        if (BeanUtil.isEmpty(productStock)) {
            return Result.success(0);
        }
        return Result.success(productStock.getUsableCount());
    }




}
